package com.ui;

import Good.Good;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;

public class X_update extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextArea textArea1;

    public JTextArea getTextArea(String str) {
//        textArea1.append("开始认证\n");
        textArea1.append(str);
        //textArea.append("sadsad55\n");
        return textArea1;
    }
    public X_update() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        buttonOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random r=new Random();
                int rand=r.nextInt(3);
                String URL = null;
                if(rand==1)
                {
                    URL="http://olddriver.3vkj.net/";
                }
                else if(rand==2)
                {
                    URL="http://olddriver2.3vdo.net";
                }
                else if(rand==0)
                {
                    URL="http://olddriver1.web3v.vip/";
                }
                Good g=new Good(textField2.getText(),
                        textField1.getText(),
                        Integer.parseInt(textField3.getText()),
                        Integer.parseInt(textField4.getText()),
                        textField5.getText(),URL);
                DENGLU.c.process(DENGLU.c.Add(g));

                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
    }

    private void onOK() {
        // 在此处添加您的代码
        String text1=textField1.getText();//商品ID
        String text2=textField2.getText();//商品名称
        String text3=textField3.getText();//商品数量
        String text4=textField4.getText();//商品价格
        String text5=textField5.getText();//商品类型

        //添加到数据库





        //dispose();
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public void main() {
        X_update dialog = new X_update();
        dialog.pack();
        dialog.setVisible(true);
        //System.exit(0);
    }
}
